package com.king.base;

import com.google.common.base.Optional;

/**
 * Created by Administrator on 2015/4/24.
 */
public class OPtionalDemo {
    public static void main(String[] args) {


        OPtionalDemo guavaTester = new OPtionalDemo();

        Integer value1 = null;
        Integer value2 = new Integer(10);
        //fromNullable为optional赋值
        Optional<Integer> a = Optional.fromNullable(value1);
        //of为optional赋值，当T为Null直接抛NullPointException,建议这个方法在调用的时候直接传常量
        Optional<Integer> b = Optional.of(value2);
        System.out.println(guavaTester.sum(a, b));
    }
    public Integer sum(Optional<Integer> a, Optional<Integer> b) {
        System.out.println("First parameter is present: " + a.isPresent());
        System.out.println("Second parameter is present: " + b.isPresent());
        Integer value1 = a.or(new Integer(0));
        Integer value2 = b.get();
        return value1 + value2;
    }
}

